home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / program / wkmc.lha / wkmc / src / plasma.c next >
C/C++ Source or Header  |  1995-10-23  |  5KB  |  174 lines

  1. /* Plasma v0.3 (23 October 1995) - ©1995 by WK-Artworks */
  2.  
  3. /*
  4.  
  5.  wkmc is a simple true-color-display using a 8-bit-screen,
  6.  the idea behind this came from Stefan Kost and Smack/IFT
  7.  
  8.  NOTE: I think it's fast beeing pure C !
  9.  
  10.  
  11.  Advantages (compared against MultiColor):
  12.   -terrible high speed
  13.   -picture-size and -aspect isn't changed
  14.   -no ugly diagonal-dithering
  15.   -no flickering (uses Multiscan)
  16.   -uses all 256 registers (not only 255)
  17.   -every color-channel (RGB) has a diffent number of
  18.    shades ("eye-sensitive")
  19. */
  20.  
  21. #include "wkmc.h"
  22.  
  23. void fSetPixel(int x,int y,double r,double g,double b) {
  24.  SetPixel(x,y,(UBYTE)(256.0*r),(UBYTE)(256.0*g),(UBYTE)(256.0*b));
  25. }
  26.  
  27. void fGetPixel(int x,int y,double *r,double *g,double *b) {
  28.  UBYTE rr,gg,bb;
  29.  GetPixel(x,y,&rr,&gg,&bb);
  30.  (*r)=(double)rr*f256;
  31.  (*g)=(double)gg*f256;
  32.  (*b)=(double)bb*f256;
  33. }
  34.  
  35. double rrmax=1.0/(double) RAND_MAX;
  36.  
  37. double drand48(void)
  38. {
  39.  return((double)rand()*rrmax);
  40.  
  41.  
  42. int main(int argc,char **argv) {
  43.  int width,height,w2,h2,qt=0;
  44.  double frac=20.0;
  45.  double aktr,aktg,aktb,c1r,c1g,c1b,c3r,c3g,c3b,c5r,c5g,c5b,c7r,c7g,c7b,c9r,c9g,c9b,f13,f14;
  46.  UWORD f[300][4],dimx,dimy;
  47.  double rndf,dimf;
  48.  WORD ptr=0;
  49.  UWORD x1,x2,x3,y1,y2,y3;
  50.  
  51.  struct IntuiMessage *imsg;
  52.  ULONG iclass;
  53.  USHORT icode;
  54.  printf("\n Plasma v0.3 - ©1995 by WK-Artworks\n");
  55.  printf(" (Plasma-algorithm by Stefan Kost)\n");
  56.  printf("------------------------------------\n");
  57.  width=320;
  58.  height=240;
  59.  if(argc<2) {printf(" Usage: plasma <frac> [LARGE]\n");return(1);}
  60.  frac=strtod(argv[1],NULL);
  61.  if(frac<=0.0) frac=20.0;
  62.  if(argc>2) {width=640;height=480;}
  63.  if(init(width,height)==0) {
  64.  printf(" Resolution : %dx%d\n",width,height);
  65.  
  66.   /* do it */
  67.   w2=width/2;h2=height/2;
  68.   f13=(double)1.0/(double)3.0;
  69.   f14=(double)1.0/(double)4.0;
  70.  
  71.   time(&tm1);
  72.   dimf=frac/((double)h2*(double)w2);
  73.   f[0][0]=0;f[0][1]=0;f[0][2]=w2-1;f[0][3]=h2-1;
  74.   aktr=drand48();aktg=drand48();aktb=drand48();
  75.   fSetPixel(0,0,aktr,aktg,aktb);
  76.   aktr=drand48();aktg=drand48();aktb=drand48();
  77.   fSetPixel(w2-1,0,aktr,aktg,aktb);
  78.   aktr=drand48();aktg=drand48();aktb=drand48();
  79.   fSetPixel(w2-1,h2-1,aktr,aktg,aktb);
  80.   aktr=drand48();aktg=drand48();aktb=drand48();
  81.   fSetPixel(0,h2-1,aktr,aktg,aktb);
  82.   while(ptr>-1) {
  83.     dimx=f[ptr][2]-f[ptr][0];
  84.     dimy=f[ptr][3]-f[ptr][1];
  85.     if(dimx>1 || dimy>1) {
  86.     rndf=(dimx*dimy)*dimf;
  87.      x1=f[ptr][0];x3=f[ptr][2];x2=x1+((x3-x1)>>1);
  88.      y1=f[ptr][1];y3=f[ptr][3];y2=y1+((y3-y1)>>1);
  89.      fGetPixel(x1,y1,&c1r,&c1g,&c1b);
  90.     fGetPixel(x3,y1,&c3r,&c3g,&c3b);
  91.     fGetPixel(x3,y3,&c9r,&c9g,&c9b);
  92.     fGetPixel(x1,y3,&c7r,&c7g,&c7b);
  93.     c5r=(c1r+c3r+c7r+c9r)*f14+(rndf*(0.5-drand48()));
  94.     if(c5r>1.0) c5r=1.0;
  95.      if(c5r<0.0) c5r=0.0;
  96.      c5g=(c1g+c3g+c7g+c9g)*f14+(rndf*(0.5-drand48()));
  97.     if(c5g>1.0) c5g=1.0;
  98.      if(c5g<0.0) c5g=0.0;
  99.      c5b=(c1b+c3b+c7b+c9b)*f14+(rndf*(0.5-drand48()));
  100.      if(c5b>1.0) c5b=1.0;
  101.     if(c5b<0.0) c5b=0.0;
  102.      fSetPixel(x2,y2,c5r,c5g,c5b);
  103.     fGetPixel(x2,y1,&aktr,&aktb,&aktg);
  104.     if(aktr==0.0 && aktg==0.0 && aktb==0.0) {
  105.      aktr=(c1r+c3r+c5r)*f13;
  106.       aktg=(c1g+c3g+c5g)*f13;
  107.      aktb=(c1b+c3b+c5b)*f13;
  108.      fSetPixel(x2,y1,aktr,aktg,aktb); 
  109.      }
  110.     fGetPixel(x1,y2,&aktr,&aktg,&aktb);
  111.     if((aktr==0.0) && (aktg==0.0) && (aktb==0.0)) {
  112.      aktr=(c1r+c7r+c5r)*f13;
  113.      aktg=(c1g+c7g+c5g)*f13;
  114.      aktb=(c1b+c7b+c5b)*f13;
  115.      fSetPixel(x1,y2,aktr,aktg,aktb); 
  116.     }
  117.      fGetPixel(x3,y2,&aktr,&aktg,&aktb);
  118.     if((aktr==0.0) && (aktg==0.0) && (aktb==0.0)) {
  119.      aktr=(c3r+c9r+c5r)*f13;
  120.       aktg=(c3g+c9g+c5g)*f13;
  121.       aktb=(c3b+c9b+c5b)*f13;
  122.       fSetPixel(x3,y2,aktr,aktg,aktb); 
  123.      }
  124.      fGetPixel(x2,y3,&aktr,&aktg,&aktb);
  125.     if((aktr==0.0) && (aktg==0.0) && (aktb==0.0)) {
  126.      aktr=(c7r+c9r+c5r)*f13;
  127.      aktg=(c7g+c9g+c5g)*f13;
  128.      aktb=(c7b+c9b+c5b)*f13;
  129.      fSetPixel(x2,y3,aktr,aktg,aktb); 
  130.     }
  131.     f[ptr][2]=x2;f[ptr][3]=y2;ptr++;
  132.     f[ptr][0]=x2;f[ptr][1]=y1;f[ptr][2]=x3;f[ptr][3]=y2;ptr++;
  133.     f[ptr][0]=x2;f[ptr][1]=y2;f[ptr][2]=x3;f[ptr][3]=y3;ptr++;
  134.     f[ptr][0]=x1;f[ptr][1]=y2;f[ptr][2]=x2;f[ptr][3]=y3;
  135.     if(ptr>250) {
  136.      printf("stack overflow\n");
  137.      ptr=-1;
  138.     }
  139.     }
  140.     else ptr--;
  141.   }
  142.  
  143.   time(&tm2);
  144.   tm=difftime(tm2,tm1);
  145.   printf(" Render-time: %lds\n",(ULONG)tm);
  146.  
  147.  
  148.   /* wait */
  149.   do {
  150.    WaitPort(theWindow->UserPort);
  151.    do {
  152.     imsg=(struct IntuiMessage*)GetMsg(theWindow->UserPort);
  153.     if(imsg!=NULL) {
  154.      iclass=imsg->Class;
  155.      icode=imsg->Code;
  156.      ReplyMsg((struct Message*)imsg);
  157.      switch(iclass) {
  158.       case IDCMP_VANILLAKEY:switch(icode) {
  159.                              case ' ':qt=1;break;
  160.                             }
  161.      }
  162.     }
  163.    } while(imsg!=NULL);
  164.   } while(qt==0);
  165.   /* clean up*/
  166.   cleanup();
  167.  }
  168.  return(0);
  169.  printf("\n");
  170. }
  171.  
  172.  
  173.